<?php
/**
 * Created by PhpStorm.
 * User: administrator
 * Date: 2016/9/2 0002
 * Time: 10:12
 */

namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html; charset=UTF-8");
//用户中心全局控制
class GoodsApiController extends  Controller{

    public function GetStockByBarCode(){
        //条形码
        $bar_code = I('request.code','','trim');
        
        //定义一个默认变量
        $sale_goods_number=0;

        if(!empty($bar_code)){

            //如果条形码不为空，根据条形码去数据库查找goods_id
            $goods_id = M('goods')->where(array('bar_code'=>$bar_code))->getField('goods_id');

            //根据goods_id查询可售库存
            $goods_number=_get_goods_cansale_count($goods_id);

            //如果商品数量大于或等于50则返回50
            if($goods_number >= 50 ){
                $sale_goods_number=50;
            }else{
                //如果商品小于50则返回正确的数值
                $sale_goods_number=$goods_number;
            }

        }


        //如果数据库没有找到该条形编码的库存则用intval将数值转为0输出
        echo intval($sale_goods_number);
        exit;
    }

    
    
    public function GetLogisticsByOrg(){ 
    	
    	$cswite=array('PCAEXPRESS'=>'WWW.PCAEXPRESS.COM.AU','BLUESKYEXPRESS'=>'WWW.BLUESKYEXPRESS.COM.AU',
    			'quancheng'=>'www.quancheng.com.au','EWE'=>'WWW.EWE.COM.AU','AOL'=>'www.aol-au.com',
    			'auexpress'=>'www.auexpress.com.au','ausexpress'=>'www.aus-express.com','zhonghuan'=>'www.zhonghuan.com.au',
    			'changjiang'=>'www.changjiangexpress.com','ECI'=>'WWW.AUSPOST.COM.AU','yundaex'=>'www.yundaex.com.au',
    			'auodexpress'=>'www.auodexpress.com','shlexp'=>'www.shlexp.com'); 
    	
    	/* $cname=array('PCAEXPRESS'=>'PCAExpress','BLUESKYEXPRESS'=>'BSE蓝天快递',
    			'quancheng'=>'申通快递','EWE'=>'EWE','AOL'=>'AOL',
    			'auexpress'=>'澳邮中国快运','ausexpress'=>'澳世速递','zhonghuan'=>'重庆中环',
    			'changjiang'=>'长江国际速递','ECI'=>'ECI','yundaex'=>'韵达快递',
    			'auodexpress'=>'澳德快递','shlexp'=>'畅灵快递'); */
    	
    	//原始订单号
    	$org_code = I('request.code','','trim'); 
        $ret="{\"state\":\"error\",\"msg\":\"提交数据错误\"}";  
    	if(empty($org_code)){ 
    		echo $ret;
    		exit; 
    	}
    	/*
    	SELECT o.*,o.order_id AS orderid,o.order_sn AS ordercode,o.org_order_sn AS org_ordercode,c.name AS companyname,b.`group_name`,c.weburl,d.invoice_no AS invoicecode FROM ki_order_info o 
    	INNER JOIN ki_delivery_order d ON o.order_id=d.order_id 
		INNER JOIN ki_expresscompany c ON c.id= d.expresscompany_id 
		INNER JOIN ki_expresscompany_group b ON c.exp_gronp_id=b.id 
		WHERE o.org_order_sn='20494735508';
    	 */
    	$dbprefix=C('DB_PREFIX');
    	$list= M("order_info")->field("o.order_id AS orderid,o.order_sn AS ordercode,o.org_order_sn AS org_ordercode,c.name AS companyname,b.`group_name`,c.weburl,b.`configurl`,d.invoice_no AS invoicecode,FROM_UNIXTIME(d.`add_time`,'%Y-%m-%d %H:%i:%s') AS add_time")
    	->alias("o")->join(" inner join ".$dbprefix."delivery_order d ON o.order_id=d.order_id")
    	->join(" inner join ".$dbprefix."expresscompany c ON c.id= d.expresscompany_id")
    	->join(" inner join ".$dbprefix."expresscompany_group b ON c.exp_gronp_id=b.id")->where("o.org_order_sn='$org_code'")->select();
       
    	if(!$list || count($list)==0){  
    		$ret="{\"state\":\"error\",\"msg\":\"没有订单记录\"}";
    		echo $ret;
    		exit; 
    	}
    	
    	$str="";
    	foreach ($list as $k=>$v){
    		//原始订单号，订单号，物流公司代号，物流公司，运输线路渠道，运单号，发货时间，物流信息访问地址。
    		$temp= $v['configurl']?str_replace("{code}", $v['invoicecode'], $v['configurl']):$v['weburl'];
    		$code=$this->Compare($cswite,$v['weburl']);  
    		$str = $str.((empty($str)?"":",")."{\"org_ordercode\":\"".$v['org_ordercode']."\",\"ordercode\":\"".$v['ordercode']."\",\"companycode\":\"".$code."\",\"companyname\":\"".$v['group_name']."\",\"line\":\"".$v['companyname']."\",\"invoicecode\":\"".$v['invoicecode']."\",\"time\":\"".$v['add_time']."\",\"url\":\"".$temp."\"}");
    	} 
    	
    	$json="[".$str."]"; 
    	$ret="{\"state\":\"success\",\"msg\":\"查询记录信息成功\",\"rows\":".$json."}";  
    	echo $ret;
    	exit; 
    } 
    
    public function Compare($ary=array(),$v=''){
    	$url=strtolower($v);
    	$key="";
    	foreach ($ary as $k=>$o){
    		$temp= strtolower($o);
    		$flg=stripos($url,$temp);
    		if($flg!==false){
    			$key=$k;
    			break;
    		}
    	}
    	return $key;
    }

}